package com.jeect.gateway4.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeect.gateway4.util.ResponseBody;
import com.jeect.gateway4.util.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class AuthURLFilter extends OncePerRequestFilter {
    private String zuulPrefix = "/api";

    private RequestCache requestCache = new HttpSessionRequestCache();
    private ObjectMapper objectMapper =new ObjectMapper();

    private String[] ALLOWURLS = {
            zuulPrefix + "/user/login",
            zuulPrefix + "/user/page/",
            zuulPrefix + "/user/get/",
            "/auth/check"
    };

    public AuthURLFilter() {
    }

    public AuthURLFilter(String zuulPrefix) {
        this.zuulPrefix = zuulPrefix;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        System.out.println("request.getRequestURI()========="+request.getRequestURI());
//        SavedRequest savedRequest = (SavedRequest) request.getSession().getAttribute("ACEGI_SAVED_REQUEST_KEY");//requestCache.getRequest(request, response);
        String url = request.getRequestURI();//savedRequest.getRedirectUrl();
        for (String s : ALLOWURLS) {
            if (s.equals(url)) {
                filterChain.doFilter(request, response);
                return;
            }
        }
        SecurityContextImpl securityContextImpl = (SecurityContextImpl)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
        System.out.println("authentication======="+authentication);
        if (authentication != null && authentication.getPrincipal()!=null) {
            Object principal = authentication.getPrincipal();
            if(principal instanceof UserDetails){
                UserDetails userDetails = (UserDetails)principal;
                if(userDetails.isAccountNonExpired()&& userDetails.isEnabled()&& userDetails.isAccountNonExpired()&& userDetails.isAccountNonLocked()){
                    filterChain.doFilter(request, response);
                    return;
                }
            }

        }
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            PrintWriter printWriter = response.getWriter();
            printWriter.write(objectMapper.writeValueAsString(new ResponseBody(ResponseCode.FAILE, "没有认证!")));

    }
}
